/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.syncro.persistencia;

import com.syncro.registro.entities.*;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;

/**
 * Implementación de los servicios de persistencia que se le prestan al sistema.
 * @author Camilo Alvarez
 */
@Stateless
public class PersistenceServices implements IPersistenceServices {

    // -----------------------------------------------
    // Atributos
    // -----------------------------------------------

    /**
     * Instancia del entity manager provista por el contenedor.
     */
    private static PersistenceServices mInstance;
    // -----------------------------------------------
    // Constructor
    // -----------------------------------------------

    public static PersistenceServices getInstance()
    {
        if(mInstance == null)
            mInstance = new PersistenceServices();        
        return mInstance;
    }

    /**
     * Constructor de la clase.
     */
    public PersistenceServices() {
       
    }

    public void create(Object obj) {
        EntityManagerFactory f = Persistence.createEntityManagerFactory("Syncro-ejbPUTest");
        EntityManager mEm = f.createEntityManager();
        try {
            mEm.getTransaction().begin();
            mEm.persist(obj);
            mEm.getTransaction().commit();
        } catch (Exception e) {
            mEm.getTransaction().rollback();
        } finally {
            mEm.close();
        }
    }

    public void update(Object obj) {
        EntityManagerFactory f = Persistence.createEntityManagerFactory("Syncro-ejbPUTest");
        EntityManager mEm = f.createEntityManager();
        try {
            mEm.getTransaction().begin();
            mEm.merge(obj);
            mEm.getTransaction().commit();
        } catch (Exception e) {
            mEm.getTransaction().rollback();
        } finally {
            mEm.close();
        }
    }

    public void delete(Object obj) {
        EntityManagerFactory f = Persistence.createEntityManagerFactory("Syncro-ejbPUTest");
        EntityManager mEm = f.createEntityManager();
        try {
            mEm.getTransaction().begin();
            mEm.remove(obj);
            mEm.getTransaction().commit();
        } catch (Exception e) {
            mEm.getTransaction().rollback();
        } finally {
            mEm.close();
        }
    }

    public List findAll(Class c) {
        EntityManagerFactory f = Persistence.createEntityManagerFactory("Syncro-ejbPUTest");
        EntityManager mEm = f.createEntityManager();
        try {
                return mEm.createQuery("select O from " + c.getSimpleName() + " as O").getResultList();
            } finally {
                mEm.close();
            }
    }

    public Object findById(Class c, Object id) {
        EntityManagerFactory f = Persistence.createEntityManagerFactory("Syncro-ejbPUTest");
        EntityManager mEm = f.createEntityManager();
        try {
                return mEm.find(c, id);
            } finally {
                mEm.close();
            }
    }

    public List buscarReportePorFecha(Date inicio, Date fin) {
        EntityManagerFactory f = Persistence.createEntityManagerFactory("Syncro-ejbPUTest");
        EntityManager mEm = f.createEntityManager();
        try {
                return mEm.createNativeQuery("select * from Registroemergencia where FechaInicio > ?1 and FechaInicio < ?2 order by Lugar asc", Registroemergencia.class).setParameter(1, inicio).setParameter(2, fin).getResultList();
            } finally {
                mEm.close();
            }
    }

    public void crearRegistro(Registroemergenciax re)
    {
        EntityManagerFactory f = Persistence.createEntityManagerFactory("Syncro-ejbPUTest");
        EntityManager mEm = f.createEntityManager();
        try {
                mEm.getTransaction().begin();
                mEm.createNativeQuery("insert into Registroemergenciax values (?1, ?2, ?3, ?4, ?5, ?6, ?7)").setParameter(1, re.getFecha()).setParameter(2, re.getHora()).setParameter(3, re.getTipoemergencia()).setParameter(4, re.getDescripcion()).setParameter(5, re.getCiudad()).setParameter(6, re.getLocalidad()).setParameter(7, re.getTelefono()).executeUpdate();
                mEm.getTransaction().commit();
               
            } 
         catch(Exception e)
                {
                    mEm.getTransaction().rollback();
                }
        finally {
                mEm.close();
            }
    }

    public Collection<String> buscarRegistroPorFecha(Date fecha) {
        EntityManagerFactory f = Persistence.createEntityManagerFactory("Syncro-ejbPUTest");
        EntityManager mEm = f.createEntityManager();
        try {
                return mEm.createQuery("select O from Registroemergenciax as O where Fecha == 1").setParameter(1, fecha).getResultList();
            } finally {
                mEm.close();
            }
    }
}

